package com.ys.peony.database;

import com.ys.peony.app.BaseApplication;
import com.zhitongyun.publics.greendao.DaoMaster;
import com.zhitongyun.publics.greendao.DaoSession;
import com.zhitongyun.publics.greendao.UserDao;

import org.greenrobot.greendao.database.Database;

/**
 * 描述：移动端数据库处理采用greenDao orm关系映射数据库
 * Created by yukang on 2017/9/12.
 */

public class DataBaseUtil {
    public static final boolean ENCRYPTED = true;
    private static final String DB_NAME = "ys_db";
    private static DaoSession mDaoSession = null;

    /**
     * 描述：初始化数据库
     */
    public static void initDataBase() {
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(BaseApplication.getInstance(), DB_NAME, null);
        DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
        mDaoSession = daoMaster.newSession();
        /*UserDao userDao = daoSession.getUserDao();
        User user = new User();
        user.setName("kk");
        user.setSex("男");
        userDao.insert(user);*/
    }

    public static DaoSession getDaoSession() {
        if(mDaoSession == null) {
            DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(BaseApplication.getInstance(), DB_NAME, null);
            DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
            mDaoSession = daoMaster.newSession();
        }
        return mDaoSession;
    }
}
